package com.cy.springsecurityoauth2demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 安全框架配置类
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //配置密码加密类
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //自定义拦截与放行
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //放行oauth授权路径，登录和退出路径
                .antMatchers("/oauth/**","/login/**","/logout/**")
                .permitAll()//以上拦截全部放行
                .anyRequest()//拦截其他所有请求
                .authenticated()//调用认证方法认证
                .and()//返回http层重新再配置其他
                .formLogin()//拦截登录表单
                .permitAll()//放行
                .and()
                .csrf().disable();//关闭csrf保护
    }
}
